package com.cx.bank.controller;

import com.cx.bank.mapper.UserMapper;
import com.cx.bank.pojo.UserBean;
import com.cx.bank.service.LoginService;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    LoginService loginService;
    @Autowired
    UserMapper userMapper;
    //跳转登录页面
    @RequestMapping("/login")
    @Before(value = "execution(public void do(..)")
    public String login(Model model,HttpSession session){

        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        UserBean user = userMapper.findUserByName(userBean.getUserName());
        model.addAttribute("currentUser",user);
        model.addAttribute("name",userBean.getUserName());
        model.addAttribute("pass",userBean.getPassword());
        return "loginPage";
    }
    //执行登录操作
    @RequestMapping("/doLogin")
    public String doLogin(@RequestParam("userName")String userName,
                          @RequestParam("password")String password,
                          @RequestParam("rememberMe")int rememberMe, Model model,
                          HttpSession session, UserBean userBean,
                          HttpServletRequest request, HttpServletResponse response){

        if (userName.equals("")) {
            model.addAttribute("msg", "请输入用户名");
            return "/loginPage";
        }
        if (password.equals("")){
            model.addAttribute("msg", "请输入密码");
            return "/loginPage";
        }
        if (userBean == null){
            model.addAttribute("msg","该用户不存在！");
            return "/loginPage";
        }
        System.out.println(rememberMe);
        if (rememberMe == 1){
            Cookie name = new Cookie("name",userName);
            Cookie pass = new Cookie("pass",password);
            name.setMaxAge(5*60);
            pass.setMaxAge(5*60);
            response.addCookie(name);
            response.addCookie(pass);

            Cookie[] cookies = request.getCookies();
            for (Cookie c : cookies) {
                {
                    System.out.println(c.getName() + " : " + c.getValue());
                }
            }
        }
        if (rememberMe == 0){
            Cookie name = new Cookie("name","");
            Cookie pass = new Cookie("pass","");
            name.setMaxAge(0);
            pass.setMaxAge(0);
            response.addCookie(name);
            response.addCookie(pass);
            Cookie[] cookies = request.getCookies();
            for (Cookie c : cookies) {
                {
                    System.out.println(c.getName() + " : " + c.getValue());
                }
            }
        }

        model.addAttribute("name",userBean.getUserName());
                model.addAttribute("pass",userBean.getPassword());
        if (loginService.login(userName,password)){
            System.out.println(userBean);
            userBean = userMapper.findUserByName(userBean.getUserName());
            session.setAttribute("currentUser",userBean);
            model.addAttribute("currentUser",userBean);
            return "redirect:user/index";
        }else {
            model.addAttribute("msg", "用户已注册");
            return "/loginPage";
        }
    }
    @RequestMapping("/logOut")
    public String logOut(Model model,HttpSession session,HttpServletRequest request, HttpServletResponse response){
        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        loginService.exitSystem(userBean);
        session.invalidate();
        request.getSession().invalidate();
        request.getCookies();
        Cookie name = new Cookie("name","");
        Cookie pass = new Cookie("pass","");
        Cookie JSESSIONID = new Cookie("JSESSIONID","");
//        model.addAttribute("JSESSIONID","");
        name.setMaxAge(0);
        pass.setMaxAge(0);
        JSESSIONID.setMaxAge(0);
        response.addCookie(name);
        response.addCookie(pass);
//        response.addCookie(JSESSIONID);
        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies) {
            {
                System.out.println(c.getName() + " : " + c.getValue());
            }
        }
        return "loginPage";
    }
}

